package com.link.play.fit.ai.ui.dialog

import android.app.Activity
import android.app.AlertDialog
import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import androidx.core.graphics.toColorInt
import com.link.play.fit.ai.R
import com.link.play.fit.ai.databinding.DialogUserAgreement2Binding
import com.link.play.fit.ai.databinding.DialogUserAgreementBinding
import com.link.play.fit.ai.utils.AppX.applicationContext
import com.link.play.fit.ai.utils.OpenAgreementUtils.openPrivacy
import com.link.play.fit.ai.utils.OpenAgreementUtils.openTerms

class UserAgreementDialog(
    val activity: Activity
) {

    private val TAG = "CongratulationDialog"
    private val dialog = AlertDialog.Builder(activity).create()
    private val binding = DialogUserAgreementBinding.inflate(LayoutInflater.from(activity))

    var nAction: (() -> Unit)? = null
    var yAction: (() -> Unit)? = null

    fun showDialog() {
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)

        dialog.show()


        dialog?.window?.setBackgroundDrawableResource(R.color.transparent)
        val params = dialog?.window?.attributes
        val marginInDp = activity.resources.getDimensionPixelOffset(R.dimen.dp_32)
        // 调整对话框宽度
        params?.width = activity.resources.displayMetrics.widthPixels - 2 * marginInDp
//        params?.gravity = Gravity.BOTTOM

        dialog?.window?.attributes = params


        val sp =
            SpannableString("请您在使用我们的服务前,仔细阅读 《用户协议》和《隐私政策》各条款, 了解个人信息的使用情况和自主选择权 利。请您知悉,您同意隐私政策仅代表 您已了解应用提供的功能,以及功能运 行所需的必要个人信息,并不代表您已 同意我们可以收集非必要个人信息,非 必要个人信息会根据您使用过程中的授 权情况单独征求您的同意。 如您同意,请点击\"同意并继续\"开始使 用我们的产品和服务。")

        // 设置《用户协议》的点击事件和颜色
        val userAgreementStart = sp.indexOf("《用户协议》")
        val userAgreementEnd = userAgreementStart + "《用户协议》".length
        sp.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 在这里处理《用户协议》的点击事件
                openTerms(activity)

            }
        }, userAgreementStart, userAgreementEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        sp.setSpan(ForegroundColorSpan("#17CB81".toColorInt()), userAgreementStart, userAgreementEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        // 设置《隐私政策》的点击事件和颜色
        val privacyPolicyStart = sp.indexOf("《隐私政策》")
        val privacyPolicyEnd = privacyPolicyStart + "《隐私政策》".length
        sp.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 在这里处理《隐私政策》的点击事件
                openPrivacy(activity)
            }
        }, privacyPolicyStart, privacyPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        sp.setSpan(ForegroundColorSpan("#17CB81".toColorInt()), privacyPolicyStart, privacyPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        binding.tvContent.movementMethod = LinkMovementMethod.getInstance() // 启用点击事件
        binding.tvContent.text = sp

        binding.tvAgree.setOnClickListener {
            dialog.dismiss()
            yAction?.invoke()
        }
        binding.tvDisAgree.setOnClickListener {

            val dialog2 = UserAgreement2Dialog(activity)
            dialog2.yAction = this.yAction
            dialog2.nAction = this.nAction
            dialog2.showDialog()


        }
    }
}

class UserAgreement2Dialog(
    val activity: Activity
) {
    private val dialog = AlertDialog.Builder(activity).create()
    private val binding = DialogUserAgreement2Binding.inflate(LayoutInflater.from(activity))

    var nAction: (() -> Unit)? = null
    var yAction: (() -> Unit)? = null

    fun showDialog() {
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)
        dialog.setCancelable(true)

        dialog.show()


        dialog?.window?.setBackgroundDrawableResource(R.color.transparent)
        val params = dialog?.window?.attributes
        val marginInDp = activity.resources.getDimensionPixelOffset(R.dimen.dp_32)
        // 调整对话框宽度
        params?.width = activity.resources.displayMetrics.widthPixels - 2 * marginInDp
//        params?.gravity = Gravity.BOTTOM

        dialog?.window?.attributes = params


        val sp =
            SpannableString("根据国家规定,需同意协议内容方可进 入应用,您可以通过阅读完整版 《用户协议》和《隐私政策》了解详细 信息。如您同意,请点击\"同意\"开始接 受我们的服务。")

        // 设置《用户协议》的点击事件和颜色
        val userAgreementStart = sp.indexOf("《用户协议》")
        val userAgreementEnd = userAgreementStart + "《用户协议》".length
        sp.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 在这里处理《用户协议》的点击事件
            }
        }, userAgreementStart, userAgreementEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        sp.setSpan(ForegroundColorSpan("#17CB81".toColorInt()), userAgreementStart, userAgreementEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        // 设置《隐私政策》的点击事件和颜色
        val privacyPolicyStart = sp.indexOf("《隐私政策》")
        val privacyPolicyEnd = privacyPolicyStart + "《隐私政策》".length
        sp.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 在这里处理《隐私政策》的点击事件
            }
        }, privacyPolicyStart, privacyPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        sp.setSpan(ForegroundColorSpan("#17CB81".toColorInt()), privacyPolicyStart, privacyPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        binding.tvContent.movementMethod = LinkMovementMethod.getInstance() // 启用点击事件
        binding.tvContent.text = sp

        binding.tvAgree.setOnClickListener {
            dialog.dismiss()
            yAction?.invoke()
        }
        binding.tvDisAgree.setOnClickListener {
            dialog.dismiss()
            nAction?.invoke()
        }
        binding.ivClose.setOnClickListener {
            dialog.dismiss()
        }
    }

}